package apii.servlet.stud;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import apii.server.ServerThings;
import apii.servlet.ServletUtils;
import apii.servlet.UserRequesterData;
import apii.utils.DoubleString;

/**
 * 
 * @author evgeniy
 * 
 */
public class ViewDisciplineServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewDisciplineServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			UserRequesterData urd = ServletUtils.extractRequesterData(request
					.getSession());
			if ((urd.sessionId == null) || !urd.logged
					|| (urd.role != UserRequesterData.ROLE_STUD)) {
				ServletUtils.redirectToUrl(response, "/login");
			} else {
				// create the list of the labs
				int discId = (Integer) request
						.getAttribute(StudRootServlet.DISCIPLINE_ID);
				if (urd.userId == UserRequesterData.UNKNOWN_ID) {
					throw new Error();
				}
				List<DoubleString> labs = getLabs(discId);
				//
				PrintWriter pw = response.getWriter();
				ServletUtils.startCommonStudDoc(pw, urd, "");
				for (DoubleString ds : labs) {
					pw.print("<a href=\"" + ServletUtils.PREFIX
							+ "/stud/all_disciplines/"
							+ ServletUtils.getDisciplineURLElement(discId)
							+ "/" + ds.urlElement + "\">" + ds.alias + "</a>");
					pw.println("<br>");
				}
				ServletUtils.closeCommonStudDoc(pw, urd);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
			ServletUtils.redirectToErrorPage(response, ex.getMessage());
		}
	}

	private List<DoubleString> getLabs(int discId) throws SQLException {
		ArrayList<DoubleString> result = new ArrayList<DoubleString>();
		Connection c = ServerThings.getThings().getConnection();
		Statement s = c.createStatement();
		ResultSet labsResultSet = s
				.executeQuery("SELECT apii.labs.lab_id, apii.labs.name "
						+ "FROM apii.labs "
						+ "WHERE apii.labs.discipline_id = '" + discId + "'");
		while (labsResultSet.next()) {
			DoubleString ds = new DoubleString(
					ServletUtils.getLabURLElement(labsResultSet.getInt(1)),
					labsResultSet.getString(2));
			result.add(ds);
		}
		c.close();

		return result;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ServletUtils.redirectToIllegalPage(response);
	}
}
